package com.empty.oredermenu.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.empty.oredermenu.R
import com.empty.oredermenu.adapter.MenuDetaliLiatAdapter
import com.empty.oredermenu.base.OrderMenuBaseActivity
import com.empty.oredermenu.httpmannage.OkHttpMannager
import com.empty.oredermenu.httpmannage.OkHttp_CallResponse
import com.empty.oredermenu.javabeen.MenuListAdapterBeen
import com.empty.oredermenu.utils.ToastUtils
import com.empty.oredermenu.utils.ToolsUtils
import com.empty.oredermenu.utils.UrlAllUtils
import kotlinx.android.synthetic.main.activity_edit_menuorder.*
import kotlinx.android.synthetic.main.layout_listview_activity.*
import okhttp3.Call
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject

/**
 * Created by empty cup on 2017/7/17.
 */

class EditMenuOrderActivity :OrderMenuBaseActivity(),OkHttp_CallResponse{


    var edit_menuorder :EditText? = null
    var menu_serach :ImageView? = null

    var detailAdapter : MenuDetaliLiatAdapter? = null
    var editBeen :ArrayList<MenuListAdapterBeen> = ArrayList()

    var imgurl :String? = null

    override fun OkHttp_ResponseSuccse(call: Call?, response: Response?, jsonObject: JSONObject?, tag: String?) {

        Log.v("order"," 搜索详情   "+jsonObject.toString())
        if (jsonObject!!.getString("code").equals("10000")){
            //只要有数据返回 就清空列表
            editBeen.clear()
            if (jsonObject!!.getString("data").equals("null")){
                ToastUtils.Toast_Long(this,"未查到菜品详细")
                return
            }

            var datajson : JSONArray = jsonObject.getJSONArray("data")
            //kotlin 闭区间写法
            for (i in 0 until datajson.length()){
                var json : JSONObject = datajson.getJSONObject(i)
                var menulistBeen :MenuListAdapterBeen = MenuListAdapterBeen()
                menulistBeen.description = json.getString("description")
                menulistBeen.food = json.getString("food")
                menulistBeen.id = json.getString("id")
                menulistBeen.keywords = json.getString("keywords")
                menulistBeen.img = json.getString("img")
                menulistBeen.name = json.getString("name")
                menulistBeen.imgs = json.getString("images")
                menulistBeen.msg = json.getString("msg")

                editBeen.add(menulistBeen)

            }
            detailAdapter!!.notifyDataSetChanged()
        }else{
            ToastUtils.Toast_Long(this,jsonObject!!.getString("message"))
        }
    }

    override fun OkHttp_CallonFailure(call: Call?) {
    }

    override fun OkHttp_ErrorCode(code: String?, tag: String?) {
    }

    override fun user_NoLogin() {
    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        toolsUtils!!.BuildColor(window, this, R.drawable.side_nav_bar)
        setContentView(R.layout.activity_edit_menuorder)
        baseInitView()
        search_init()
    }

    fun search_init(){
        layout_title!!.text = "搜索"

        menu_serach = findViewById(R.id.menu_serach) as ImageView
        edit_menuorder = findViewById(R.id.edit_menuorder) as EditText

        detailAdapter = MenuDetaliLiatAdapter(this,editBeen)
        list_search.adapter = detailAdapter
        list_search.setOnItemClickListener { _, _, position, _ ->
            if (editBeen.size>0){
                var intent = Intent()
                intent.setClass(this@EditMenuOrderActivity,MenuDetailActivity::class.java)
                intent.putExtra("listbeen",editBeen.get(position))
                startActivity(intent)
            }
        }

    }

    fun onSearch(view: View){
        if (edit_menuorder != null){

            http_Search(edit_menuorder!!.text.toString())
        }

    }
    fun http_Search(name :String){

        val url: String =
                UrlAllUtils.URL_MENUNAME_ADDRESS +
                        "?key="+ UrlAllUtils.URL_MENUNAME_KEY+
                        "&name="+name

        OkHttpMannager.getInstance().postAsynHttp(url,this@EditMenuOrderActivity,this,false,"menusearch")
    }
}
